package com.example.demo.codegenerate.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Template;
import freemarker.template.TemplateException;

import static java.nio.file.StandardOpenOption.*;

/**
 * @author thing
 * 2017年5月12日
 */
public class MyFreeMarkerTemplateUtils extends FreeMarkerTemplateUtils {

	public static void processTemplateIntoFile(Template template,
			Object model, String filePath)
			throws IOException, TemplateException {
		File file = new File(filePath);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		BufferedWriter bw = Files.newBufferedWriter(file.toPath(), Charset.forName("UTF-8"), CREATE, TRUNCATE_EXISTING, WRITE);
		
		template.process(model, bw);
		bw.close();
	}
}
